package com.gscitysfy.cus.drain.modules.inspection.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gscitysfy.cloudfrmwk.assistant.domain.Result;
import com.gscitysfy.cus.drain.annotations.DictTranslation;
import com.gscitysfy.cus.drain.annotations.FieldParam;
import com.gscitysfy.cus.drain.constants.DicConstant;
import com.gscitysfy.cus.drain.modules.inspection.entity.DrainInspectionMnPlan;
import com.gscitysfy.cus.drain.modules.inspection.service.IDrainInspectionMnPlanService;
import com.gscitysfy.cus.drain.modules.inspection.vo.PlanVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author Administrator
 * @since 2022-10-14 15:00:46
 */
@RestController
@RequestMapping("/inspection/plan")
@Tag(name = "巡检计划-》PC端相关接口")
public class DrainInspectionMnPlanController {

    @Resource
    private IDrainInspectionMnPlanService planService;

    @Resource
    private HttpServletResponse response;

    /**
     * 修改或者保存巡检计划
     * @param plan
     * @return
     */
    @PostMapping("/add-plan")
    @Operation(summary = "修改或者保存巡检计划")
    @Parameters({
            @Parameter(required = true, name = "cmPlanName", description = "计划名称"),@Parameter(required = true, name = "cmPlanObjectType", description = "计划对象类型"),
            @Parameter(required = true, name = "cmPlanBasis", description = "计划依据"),@Parameter(required = true, name = "cmPlanType", description = "计划类型"),
            @Parameter(required = true, name = "cmPlanFrequency", description = "计划频次"),@Parameter(required = true, name = "cmPlanStartDate", description = "计划周期开始时间"),
            @Parameter(required = true, name = "cmPlanEndDate", description = "计划周期结束时间"), @Parameter(required = true, name = "cmPlanHeaderName", description = "计划负责人名称"),
            @Parameter(required = true, name = "cmPlanHeaderId", description = "计划负责人id"), @Parameter(required = true, name = "userList", description = "计划执行人", example = "[{\"cmUserId\": \"1\", \"cmUserName\":\"赵\", \"cmUserPhone\":\"13698773456\"}]"),
            @Parameter(required = true, name = "areaPoints", description = "区域点位集合", example = "[{\"cmPointLng\": \"1.1111\", \"cmPointLat\":\"1.111\"}]"),
            @Parameter(required = true, name = "objectPoints", description = "巡检对象集合", example = "[{\"cmPointLng\": \"1.1111\", \"cmPointLat\":\"1.111\"}]"),
            @Parameter(required = true, name = "cmPlanCategory", description = "计划分类，0巡检计划 1 养护计划 2检查计划"),
    })
    public Result<Boolean> addOrUpdate(@Parameter(hidden = true)@RequestBody DrainInspectionMnPlan plan){
        return planService.addOrUpdate(plan);
    }

    /**
     * @param ids
     * @return
     */
    @GetMapping("/del-plan")
    @Operation(summary = "删除巡检计划（支持批量删除）")
    @Parameters({
            @Parameter(required = true, name = "ids", description = "巡检计划ids，多个使用逗号分隔")
    })
    public Result<Boolean> remove(@RequestParam("ids") String ids){
        return planService.remove(ids);
    }

    /**
     * 获取巡检计划详情
     * @param id
     * @return
     */
    @GetMapping("/view")
    @Operation(summary = "获取巡检计划详情")
    @Parameters({
            @Parameter(required = true, name = "id", description = "巡检计划id")
    })
    @DictTranslation({
            @FieldParam(dictType = DicConstant.PLAN_BASIS, targetField = "cmPlanBasis"),
            @FieldParam(dictType = DicConstant.PLAN_TYPE, targetField = "cmPlanType"),
            @FieldParam(dictType = DicConstant.PLAN_FREQUENCY, targetField = "cmPlanFrequency"),
            @FieldParam(dictType = DicConstant.PLAN_OBJECT_TYPE, targetField = "cmPlanObjectType")
    })
    public Result<DrainInspectionMnPlan> viewById(@RequestParam("id") Long id){
        return planService.viewById(id);
    }

    /**
     * 获取巡检计划列表
     * @return
     */
    @Operation(summary = "获取巡检计划列表")
    @PostMapping("/list")
    @Parameters({
            @Parameter(required = false, name = "cmPlanBasis", description = "计划依据"), @Parameter(required = false, name = "cmPlanType", description = "计划类型"),
            @Parameter(required = false, name = "cmPlanFrequency", description = "计划频次"), @Parameter(required = false, name = "cmPlanApprovalStatus", description = "审核状态"),
            @Parameter(required = false, name = "cmPlanNo", description = "计划编号"), @Parameter(required = false, name = "cmPlanName", description = "计划名称"),
            @Parameter(required = false, name = "startTime", description = "计划开始时间"), @Parameter(required = false, name = "endTime", description = "计划结束时间"),
            @Parameter(required = false, name = "cmPlanHeaderName", description = "计划负责人"), @Parameter(required = false, name = "cmPlanExecutorName", description = "计划执行人"),
            @Parameter(required = false, name = "cmPlanObjectType", description = "计划对象类型"), @Parameter(required = false, name = "cmPlanIsCompleted", description = "计划是否已完成"),
            @Parameter(required = false, name = "isPage", description = "类别是否分页，默认分页"), @Parameter(required = false, name = "size", description = "分页没页条数"),
            @Parameter(required = false, name = "current", description = "当前页"), @Parameter(required = true, name = "cmPlanCategory", description = "计划分类，0巡检计划 1 养护计划 2检查计划"),
    })
    @DictTranslation({
            @FieldParam(dictType = DicConstant.PLAN_BASIS, targetField = "cmPlanBasis"),
            @FieldParam(dictType = DicConstant.PLAN_TYPE, targetField = "cmPlanType"),
            @FieldParam(dictType = DicConstant.PLAN_FREQUENCY, targetField = "cmPlanFrequency"),
            @FieldParam(dictType = DicConstant.PLAN_OBJECT_TYPE, targetField = "cmPlanObjectType")
    })
    public Result<Object> list(@Parameter(hidden = true)@RequestBody(required = false) PlanVo plan){
        Page<DrainInspectionMnPlan> page = new Page<>();
        if(Objects.isNull(plan)){
            plan =  new PlanVo();
        }
        page.setCurrent(plan.getCurrent());
        page.setSize(plan.getSize());
        return planService.getPlanList(page, plan);
    }



    /**
     * 审核巡检计划
     * @param plan
     * @return
     */
    @Operation(summary = "审核巡检计划")
    @PostMapping("/audit")
    @Parameters({
            @Parameter(required = true, name = "cmKid", description = "巡检计划id"),
            @Parameter(required = true, name = "cmPlanApprovalStatus", description = "巡检计划状态，0待审核，1审核通过，2审核通过"),
            @Parameter(required = false, name = "cmPlanApprovalComments", description = "审核意见"),
    })
    public Result<Boolean> audit(@RequestBody PlanVo plan){
        return planService.audit(plan);
    }


    /**
     * 导出巡检计划
     * @param plan
     */
    @PostMapping("/export")
    @Operation(summary = "导出巡检计划")
    @Parameters({
            @Parameter(required = false, name = "cmPlanBasis", description = "计划依据"), @Parameter(required = false, name = "cmPlanType", description = "计划类型"),
            @Parameter(required = false, name = "cmPlanFrequency", description = "计划频次"), @Parameter(required = false, name = "cmPlanApprovalStatus", description = "审核状态"),
            @Parameter(required = false, name = "cmPlanNo", description = "计划编号"), @Parameter(required = false, name = "cmPlanName", description = "计划名称"),
            @Parameter(required = false, name = "startTime", description = "计划开始时间"), @Parameter(required = false, name = "endTime", description = "计划结束时间"),
            @Parameter(required = false, name = "cmPlanName", description = "计划负责人"), @Parameter(required = false, name = "cmPlanBasis", description = "计划执行人"),
            @Parameter(required = false, name = "cmPlanObjectType", description = "计划对象类型"), @Parameter(required = false, name = "ids", description = "计划id，多个使用逗号分隔")
    })
    public void export(@RequestBody(required = false) PlanVo plan) throws IOException {
        if(Objects.isNull(plan)){
            plan =  new PlanVo();
        }
        planService.export(response, plan);
    }




}
